// 获取全局APP
const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    myhost: app.globalData.remoteDomainMy,
    activeHealth:"",
    activeYounth: "",
    activeScience: "",
    activePeople: "",
    activeChild: "",
    activeHotsale: "",
    activefreepost: "",
    activeNewbook: "",
  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 加载loading
    wx.showLoading({
      title: '加载中',
    })

    //定义this指向
    var that = this;

    //是搜索还是查询分类？
    if (options.tap) {//查询分类
      //获取TAP
      let tap = options.tap;
      this.classify(tap);
      
    } else if(options.keywords){//搜索
      //搜索词
      let searchKeywords = options.keywords;
      //查询的哪个字段
      let comlumn = options.action
      //后端查询
      //根据options,从服务器拿对应的数据
      wx.request({
        url: app.globalData.remoteDomainApi + 'booklist.php',
        data: {
          searchKeywords: searchKeywords,
          comlumn: comlumn
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          //取消loading
          wx.hideLoading();

          // //给模板提供数据
          // console.log(res);

          that.setData({
            list: res.data
          })
        }
      }) 


    }
  },

 /**
 * 分类函数
 */
classify:function(tap){
  var that = this;
  //初始化变量
  let res = '';

  // 处理分类样式
  switch (tap) {
    case 'science':
      this.setData({
        activeScience: 'active',
        activeHealth: "",
        activeYounth: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'health':
      this.setData({
        activeHealth: 'active',
        activeYounth: "",
        activeScience: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'culture':
      this.setData({
        activeYounth: 'active',
        activeHealth: "",
        activeScience: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'people':
      this.setData({
        activePeople: 'active',
        activeHealth: "",
        activeYounth: "",
        activeScience: "",
        activeChild: "",
        activeHotsale: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'child':
      this.setData({
        activeChild: 'active',
        activeHealth: "",
        activeYounth: "",
        activeScience: "",
        activePeople: "",
        activeHotsale: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'hotsale':
      this.setData({
        activeHotsale: 'active',
        activeHealth: "",
        activeYounth: "",
        activeScience: "",
        activePeople: "",
        activeChild: "",
        activefreepost: "",
        activeNewbook: ""
      })
      break;

    case 'freeposter':
      this.setData({
        activefreepost: 'active',
        activeHealth: "",
        activeYounth: "",
        activeScience: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "",
        activeNewbook: ""
      })
      break;

    case 'newbook':
      this.setData({
        activeNewbook: 'active',
        activeHealth: "",
        activeYounth: "",
        activeScience: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "",
        activefreepost: ""
      })
      break;  

    case 'bookmoretop':
      this.setData({
        activeScience: "",
        activeHealth: "",
        activeYounth: "",
        activePeople: "",
        activeChild: "",
        activeHotsale: "active",
        activefreepost: "",
        activeNewbook: ""
      })
      break;  
  }

  //根据tap,从服务器拿对应的数据
  wx.request({
    url: app.globalData.remoteDomainApi + 'booklist.php',
    data: {
      tap: tap
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success(res) {
      //取消loading
      wx.hideLoading();

      that.setData({
        list: res.data
      })
    }
  }) 
},

/**
 * 分类数据
 */
  classitem:function(e){
    console.log(e)
    let tap = e.currentTarget.dataset.tap;
    this.classify(tap)
  },

  /**
 * 详情页
 */
  gobookdetail: function (e) {
    //获取id
    let id = e.currentTarget.dataset.id;

    //跳转到详情页
    wx.navigateTo({
      url: '../detailBook/detailBook?id='+id,
    })
  }
})